import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class PwdHintText extends StatefulWidget {
  final TextEditingController? controller;
  final String? hintText;
  final int? maxLength; //最大输入长度
  final double hintSize; //提示文字尺寸
  final FocusNode? focusNode;
  final TextInputType? textInputType;
  final double fontSize;
  final FontWeight? fontWeight;
  final FontWeight? hintFontWeight;
  final Color? inputColor; // 输入信息颜色
  final Color? hintColor; // 提示信息颜色

  const PwdHintText({
    super.key,
    this.controller,
    this.hintText,
    this.maxLength,
    this.hintSize = 14.0,
    this.focusNode,
    this.textInputType = TextInputType.streetAddress,
    this.fontSize = 14.0,
    this.fontWeight,
    this.hintFontWeight,
    this.inputColor,
    this.hintColor = Colors.black26,
  });

  @override
  State<PwdHintText> createState() => _PwdHintTextState();
}

class _PwdHintTextState extends State<PwdHintText> {
  bool? _isShowPwdDisplay = false; //是否显示密码
  var _isShowPwd = false;

  void _focusNodeListener() {
    bool? isHasFocus = widget.focusNode?.hasFocus;

    if (isHasFocus != _isShowPwdDisplay) {
      setState(() {
        _isShowPwdDisplay = isHasFocus;
      });
    }
  }

  @override
  void initState() {
    super.initState();
    widget.focusNode?.addListener(_focusNodeListener);
  }

  @override
  void dispose() {
    widget.focusNode?.removeListener(_focusNodeListener);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return TextField(
      maxLength: widget.maxLength,
      focusNode: widget.focusNode,
      controller: widget.controller,
      keyboardType: widget.textInputType,
      textAlignVertical: TextAlignVertical.center,
      inputFormatters: [
        FilteringTextInputFormatter.deny(RegExp("[\u4e00-\u9fa5]"))
      ],
      style: TextStyle(
        fontSize: widget.fontSize,
        fontWeight: widget.fontWeight,
        color: widget.inputColor,
      ),
      decoration: InputDecoration(
        border: InputBorder.none,
        hintText: widget.hintText,
        counterText: "",
        hintStyle: TextStyle(
          color: widget.hintColor,
          fontSize: widget.hintSize,
          fontWeight: widget.hintFontWeight,
        ),
        suffixIcon: _isShowPwdDisplay!
            ? IconButton(
                icon: Icon(
                    (_isShowPwd) ? Icons.visibility : Icons.visibility_off,
                    color: Colors.black26),
                onPressed: () {
                  setState(() {
                    _isShowPwd = !_isShowPwd;
                  });
                },
              )
            : const IconButton(icon: Icon(null), onPressed: null),
        // 是否显示密码
      ),
      obscureText: !_isShowPwd,
    );
  }
}
